library(tidyverse)
library(fixest)
library(showtext)
library(vtable)
library(writexl)
library(haven)
library(gridExtra)

setwd('D:\\WorkingPaper-Series\\Township_Light\\Replication_Files\\workdata')

#################################
########## Figure 3 #############
#################################

event_study <- read_delim('event_study.csv')

event_study <- event_study %>% mutate(upper_ci = coef + 1.96*std_err, lower_ci = coef - 1.96*std_err)

event_study %>% filter(relative_time<=5) %>% ggplot(aes(relative_time, coef, group = type, color = type)) +
  geom_hline(yintercept = 0,color = 'black', lwd = 0.5, lty = 2)+
  geom_vline(xintercept = -1, color = 'grey50', lwd = 0.5, lty = 4)+
  geom_errorbar(aes(ymin = lower_ci, ymax = upper_ci), width=.5, lwd = .3,position=position_dodge(0.5))+
  geom_point(aes(shape = type, color = type, fill = type),position=position_dodge(0.5)) +
  theme_bw()+
  theme(legend.position = c(0.12,0.25),
        legend.background = element_rect(color = 'black', size = 0.1,linewidth = 0.1, fill = 'white'),
        legend.margin = margin(t = -0.35, r = 0.1, b = 0, l = 0.1, unit = "cm"),
        text = element_text(family = 'serif',size = 8),
        axis.text =  element_text(family = 'serif',size = 8))+
  labs(y = '', x = 'Relative Time') +
  scale_x_continuous(breaks = c(seq(-5,5,1)), labels = c('-5+','-4','-3','-2','-1','0','1','2','3','4','5+')) +
  scale_y_continuous(limits = c(-0.2,0.1))+
  scale_color_manual(values = c('twfe' = '#080202', 'dCDH 2020' = 'grey30', 'SA 2020' = 'grey60', 'CS 2021' = 'grey80'),
                     labels = c('twfe' = 'TWFE OLS', 'dCDH 2020' = 'dCDH 2020', 'SA 2020' = 'SA 2020', 'CS 2021' = 'CS 2021')) +
  scale_fill_manual(values = c('twfe' = '#080202', 'dCDH 2020' = 'grey30', 'SA 2020' = 'grey60','CS 2021' = 'grey80'),
                    labels = c('twfe' = 'TWFE OLS', 'dCDH 2020' = 'dCDH 2020', 'SA 2020' = 'SA 2020', 'CS 2021' = 'CS 2021')) +
  scale_shape_manual(values = c('twfe' = 21, 'dCDH 2020' = 23, 'SA 2020' = 15,  'CS 2021' = 17),
                     labels = c('twfe' = 'TWFE OLS', 'dCDH 2020' = 'dCDH 2020', 'SA 2020' = 'SA 2020','CS 2021' = 'CS 2021')) +
  guides(colour = guide_legend(""),fill = guide_legend(""),shape = guide_legend(""))

